package de.hsowl.tod4a.global.query;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * 
 * @author Sebastian Witt
 *
 * @param <T>
 */
public abstract class Comparator<T> implements Serializable {

	private static final long serialVersionUID = 1106383843914980987L;
	private Class<T> classType;
	private String methodName = "compare";

	public Comparator() {
		setClassType(getClassName());
	}

	/**
	 * Selects the objects which will returned
	 * @param comperator
	 * @return true->Object will returned; false->object will not given back
	 */
	public abstract boolean compare(T object);

	/**
	 * Tries to figure out which class is T in the method compare().
	 * @return
	 */
	public Class<T> getClassName() {
		Method[] methods = getClass().getMethods();
		for (Method method : methods) {
			if ((!method.getName().equals(methodName)) || (method.getParameterTypes().length != 1)) {
				continue;
			}
			@SuppressWarnings("unchecked")
			Class<T> targetClass = (Class<T>) method.getParameterTypes()[0];
			if (targetClass != null && targetClass.getName().equals("") == false) {
				classType = targetClass;
				return targetClass;
			}
		}
		return null;
	}

	public Class<T> getClassType() {
		return classType;
	}

	public void setClassType(Class<T> classType) {
		this.classType = classType;
	}

}
